import {createRouter, createWebHashHistory, type RouteRecordRaw} from 'vue-router'
import Main from '../layout/Main.vue'
import {Avatar, Calendar, Document, Management, MessageBox, OfficeBuilding, Van} from "@element-plus/icons-vue";
import {Permissions} from "../types/permissions.ts";

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Main,
    redirect: '/login',
    children: [
      {
        path: '/company',
        name: 'Company',
        component: () => import('../views/company/index.vue'),
        meta: {
          title: '公司管理',
          icon: Management,
          permission: Permissions.company
        }
      },
      {
        path: '/staff',
        name: 'Staff',
        component: () => import('../views/personal/index.vue'), // 临时使用现有组件
        meta: {
          title: '人员管理', icon: Avatar,
          permission: Permissions.personnel
        }
      },
      {
        path: '/car',
        name: 'Car',
        component: () => import('../views/Car/index.vue'), // 临时使用现有组件
        meta: {
          title: '车辆管理', icon: Van,
          permission: Permissions.car
        }
      },
      {
        path: '/device',
        name: 'Device',
        component: () => import('../views/device/index.vue'), // 临时使用现有组件
        meta: {
          title: '物料设备管理', icon: MessageBox,
          permission: Permissions.device
        }
      },
      {
        path: '/department',
        name: 'Department',
        component: () => import('../views/department/index.vue'),
        meta: {
          title: '部门管理', icon: OfficeBuilding,
          permission: Permissions.department
        }
      },
      {
        path: '/project',
        name: 'Project',
        component: () => import('../views/project/index.vue'),
        meta: {
          title: '项目管理', icon: Calendar,
          permission: Permissions.project
        }
      },
      {
        path: '/record',
        name: 'Record',
        redirect: '/record/car',
        meta: {
          title: '使用记录查询',
          permission: Permissions.all,
          icon: Document
        },
        children: [
          {
            path: '/record/car',
            name: 'CarRecord',
            component: () => import('../views/record/car/index.vue'),
            meta: {
              title: '车辆使用记录',
              permission: Permissions.car
            }
          },
          {
            path: '/record/device',
            name: "DeviceRecord",
            component: () => import('../views/record/device/index.vue'),
            meta: {
              title: "设备使用记录",
              permission: Permissions.device
            }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/login/index.vue")
  },
  {
    path: "/dashboard",
    name: "dashboard",
    component: () => import("../views/dashboard/index.vue")
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 添加路由守卫
router.beforeEach((to, _from, next) => {
  // 获取用户信息
  const userStore = JSON.parse(localStorage.getItem('user') || '{}');
  const isLoggedIn = userStore?.userInfo !== undefined;
  
  // 如果要去的是登录页或用户已登录，则允许访问
  if (to.path === '/login' || isLoggedIn) {
    next();
  } else {
    // 否则重定向到登录页
    next('/login');
  }
});

export default router